package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Article;
import com.ultrasoft.farmasoft.domain.Region;

@Repository(value = "articleDao")
@Transactional(readOnly = false)
public class ArticleDaoImpl implements ArticleDao {
	
	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	@SuppressWarnings("unchecked")
	public List<Article> getArticleList() {
		return em.createNamedQuery("Article.findAll").getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Article> getArticleList(Integer idArticle){
		Query  q = em.createNamedQuery("Article.findByActive");
    	q.setParameter("articleId",idArticle);
    	return q.getResultList();	
	}
	@SuppressWarnings("unchecked")
	public List<Article> getArticleListWithIdRegion(Integer idRegion){
		Query  q = em.createNamedQuery("Article.findByArticleIdWithIdRegion");
    	//q.setParameter("active",active);
    	q.setParameter("regionId",idRegion);
    	return q.getResultList();	
	}
	
	@SuppressWarnings("unchecked")
	public List<Article> getArticleListUserWithIdRegionActive(Integer idRegion,boolean active){
		Query  q = em.createNamedQuery("Article.findByRegionIdActive");
    	q.setParameter("regionId",idRegion);
    	q.setParameter("active",active);
    	//q.setParameter("active2",true);
    	return q.getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Article>  getArticleListIdArticleActive(Integer idArticle,boolean active){
		Query  q = em.createNamedQuery("Article.findByArticleIdActive");
    	q.setParameter("articleId",idArticle);
    	q.setParameter("active",active);
    	//q.setParameter("active2",true);
    	return q.getResultList();
	}
	public List<Article> getArticleListUser(Integer createUserId) {
		Query  q = em.createNamedQuery("Article.findByCreateUserId");
    	q.setParameter("createUserId",createUserId);
    	return q.getResultList();	
	}
	//From the distinct region
	public List<Article> getArticleDistintRegionId(){
		return em.createNamedQuery("Article.ArticleDistinctRegionId").getResultList();
	}
	public List<Article> getArticleDistinctRegionRegains(Integer idRegion) {
		Query  q = em.createNamedQuery("Article.ArticleDistinctRegionRegains");
    	q.setParameter("regionId",idRegion);
    	return q.getResultList();
	}
	
	/*@SuppressWarnings("unchecked")
	public List<Article> getArticleListExcuseIdArticle(Integer idArticle,boolean active){
		Query  q = em.createNamedQuery("Article.findExcuseIdArticleByActive");
    	q.setParameter("active",active);
    	q.setParameter("articleId",idArticle);
    	return q.getResultList();	
	}*/
	
	public Article findArticleByID(Integer idArticle) {
		Article res=null;
		try {
	    	res=em.find(Article.class,idArticle);
		} catch (Exception e) {}
    	return res;
	}
	public void add(Article article) {
		em.persist(article);
	}
	public void update(Article article) {
		em.merge(article);
	}

	public void remove(Article article) {
		//Query q = em.createNamedQuery("Region.deleteByCountryIdByRegionId");
    	//q.executeUpdate();
		em.remove(em.getReference(Region.class,article.getRegionId()));	
	}
	
	
	
}
